<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/5/7
 * Time: 15:00
 */

namespace app\admin\controller;

use app\admin\model\Menu as MenuModel;

class Menu extends Base
{
    private $model;
    public function _initialize()
    {
        $this->model = new MenuModel();
    }

    public function index(){

        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            $where['status'] = [['eq',0],['eq',1], 'or'];
            $where['pid'] = 0;
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $selectResult = $this->model->getMenuByWhere($where, $offset, $limit);


            $status = config('code.status_normal');

            //拼接参数
            foreach ($selectResult as $k => $v) {
                $selectResult[$k]['status'] = $status[$v['status']];
//                $selectResult[$k]['url'] = '<img src="' . $v['url'] . '" width="40px" height="40px">';
                $selectResult[$k]['operate'] = showOperate($this->makeButton($v['id']));
            }
            $result['total'] = $this->model->getAllMenu($where);//总数据
            $result['rows'] = $selectResult;
//            dump($result);die;
            return json($result);
        }
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $param = input('post.');
            $flag = $this->model->addMenu($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign('status',config('code.status_normal'));
        return $this->fetch();
    }

    public function edit()
    {
        if(request()->isPost()){
            $param = input('post.');

            $flag = $this->model->editMenu($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $data = $this->model->getOneMenu($id);

        $this->assign([
            'data' => $data,
            'status'=>config('code.status_normal')
        ]);
        return $this->fetch();
    }

    public function uploadImg()
    {
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload/gift');
            if($info){
                $src =  '/upload/gift' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

    public function del(){
        if ($this->request->isGet()) {
            $id = input('param.id');
            $flag = $this->model->saveStatus($id);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'Menu/edit',
                'href' => url('Menu/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'Menu/del',
                'href' => "javascript:adminDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
            '子选项列表' => [
                'auth' => 'Menu_content/index',
                'href' => url('Menucontent/index', ['id' => $id]),
                'btnStyle' => 'success',
                'icon' => 'fa fa-clipboard'
            ]
        ];
    }
}